﻿/*
 *			钓鱼场工作人员
 *
 */
var aaa = "#fUI/UIWindow.img/createCygnus/BtLeft/mouseOver/0#";
var aaa1 = "#fUI/UIWindow.img/createCygnus/BtRight/normal/0#";
var aaa2 = "#fUI/UIWindow2.img/MapleStyle/RewardPopup/BtReward/mouseOver/0#";
var aaa3 = "#fUI/UIWindow2.img/MapleStyle/RewardPopup/BtGoCash/pressed/0#"
var aaa4 = "#fUI/UIWindow2.img/MasterPiece/Effect/Label/Master/2#"
var aaa5 = "#fUI/UIWindow2.img/MonsterBook/LeftTab/0/new_n/1#"
var p1 = "#fUI/Basic.img/BtCoin/normal/0#";
var p4 = "#fUI/StarCityUI.img/GradeInfo/icon_ss/1#"
var p2 = "#fUI/SoulUI.img/DungeonMap/icon/soulFragment/0#"
var q = "#fUI/CashShop.img/CSEffect/event/0#"
var q1 = "#fUI/CashShop.img/CSEffect/mileage/0#"
var p3 = "#fUI/SoulUI.img/DungeonMap/icon/dungeonItem/0#"

var mChar = function(client) {

    this.db = cm.getConnection();
    //获取积分
    this.getFishIntegral = function(player) {
            var ps = this.db.prepareStatement("SELECT dyjf FROM `characters` WHERE `id` = ?");
            ps.setInt(1, player.getId());
            var rs = ps.executeQuery();
            var key = 0;
            if (!rs.next()) return false;
            key = rs.getInt(1);
            rs.close();
            ps.close();
            return (key == '' || key == null) ? 0 : key;
        }
        //增加积分
    this.updateFishIntegral = function(numbers, player) {
        if (this.getFishIntegral(player) == false) {
            //原来无积分
            var ps = this.db.prepareStatement("UPDATE `characters` SET dyjf = ? WHERE `id` = ?");
            ps.setInt(1, 0);
            ps.setInt(2, player.getId());
            ps.executeUpdate();
            ps.close();
        }
        var ps = this.db.prepareStatement("UPDATE `characters` SET dyjf = ? WHERE `id` = ?");
        ps.setInt(1, numbers + this.getFishIntegral(player));
        ps.setInt(2, player.getId());
        ps.executeUpdate();
        ps.close();
        return;
    };
    // this.get_Ranks = function(numbers, player) {
    //     var ps = this.db.prepareStatement("SELECT `name`,`dyjf`  FROM characters ORDER BY dyjf DESC LIMIT 0,30");
    //     var rs = ps.executeQuery();
    //     var result = "钓鱼排行榜前 #r#e" + numbers + " #n#k 名玩家:\r\n\r\n";
    //     var i = 1;
    //     while (rs.next() && i <= numbers) {
    //         result += "#b第 " + i + " 名：" + rs.getString(1) + "\t\t\t积分：" + rs.getString(2) + ".";
    //         result += "\r\n";
    //         i++;
    //     }
    //     if (!i) {
    //         result += "\r\n#b抱歉，暂时无玩家排行.";
    //     }
    //     rs.close();
    //     ps.close();
    //     return result;
    // };
    //debug
    // this.debug = function(str) {
    //     org.slf4j.LoggerFactory.getLogger(this).info(str);
    // };
};

//importPackage(net.sf.odinms.server);
//importPackage(java.util);
//importPackage(net.sf.odinms.client);
var status;
var price1 = 1 * 100000; //鱼饵价格 (冒险币)
var price2 = 20000; //高级鱼饵价格 (元宝)
var price_st = -1;
var choose = -1;
var chooses = -1;
var str_name = '';
var num = -1;
var db;
var player;

function start() {
    status = -1;
    db = new mChar(cm.getC());
    action(1, 0, 0);
}

function action(mode, type, selection) {
    player = cm.getPlayer();
    if (mode == -1) {
        cm.dispose();
    } else {
        if (mode == 0 && status >= 0) {
            cm.dispose();
            return;
        }
        if (mode == 1) status++;
        else status--;
    }
    if (status == 0) {
        var str = "               " + aaa + "  #e#r每周钓鱼排名赛  " + aaa1 + "#n\r\n\r\n";
        str += " " + q + "" + q1 + "" + q + "" + q1 + "" + q + "" + q1 + "" + q + "" + q1 + "" + q + "" + q1 + "\r\n";
        str += "#e#r#L6#" + p3 + " 市场钓鱼解答？#l\r\n";
        str += "#L551##r" + p3 + " 进入钓鱼地图#n#l\r\n";
        str += "#L15##d" + p3 + " 购买高级鱼竿(永久_5W点卷)#l\r\n";
        str += "#L2##d#e" + p3 + " 购买高级鱼饵#n#r（高级鱼饵2倍概率）#b#l\r\n";
        str += "#L3##e#r" + p3 + " 使用各种鱼兑换成硬币#b\r\n";
        str += "#L4##e#r" + p3 + " 使用硬币兑换物品#b\r\n";
        str += "#L99##e#r" + p3 + " 使用钓鱼积分兑换物品#b\r\n\r\n\r\n";
        cm.sendSimple(str);
    } else if (status == 1) {
        choose = selection;
        if (selection == 15) { //买高级鱼竿
            cm.sendYesNo("高级鱼竿的样子是 #v5340001# ,使用高级鱼竿，可以在极速钓鱼场里钓鱼哦！当然，鱼饵也是必要的！\r\n\r\n\r\n你是否需要买？\r\n\r\n价格：#b 5W点卷/杆/永久;");
        }
        if (selection == 2) {
            cm.sendGetText("高级鱼饵的样子是 #v2300001# ,鱼类最爱吃的鱼饵，可以大幅度增加收获量.\r\n 每 " + price2 + "金币/1条.\r\n,您需要(#b请输入条数#k#r（注意:请确保消耗窗有空位 1组=120条）):");
        }
        if (selection == 3) {
            cm.sendNext("该操作是将所有钓到的鱼换成硬币,是否继续？");
        }
        if (selection == 4) {
            cm.dispose();
            cm.openNpc(9330122, 9);
        }
        if (selection == 6) {
            cm.sendNext("#e#d本服高级钓鱼竿为急速钓鱼30秒一次 普通钓鱼竿为60秒一次,每周排行榜大奖等你拿.买了鱼杆和鱼饵在自由市场坐个椅子就能钓,屏幕中间会有提示,不行的请小退游戏再上即可，钓鱼有机率可获得：\r\n#v4310030##v4310036##v4033356##v4021012##v4021011##v4021010##v4000082##v4000124##v4310015##v4021019##v4009096##v4021022##v4021021##v4021020##v4000435#");
            cm.dispose();
        }
        if (selection == 551) {
            cm.dispose();
            //cm.openNpc(9330110);
            cm.openNpc(9330122, 1);
        }
        if (selection == 99) {
            cm.dispose();
            //cm.openNpc(9330110);
            cm.openNpc(9330122, 8);
        }
    } else if (status == 2) {
        if (choose == 15) { //高级鱼竿 永久
            if (cm.getNX(1) > 50000 && cm.canHold(5340001)) {
                cm.gainNX(1, -50000);
                cm.gainItem(5340001, 1);
                cm.sendOk("成功购买.");
            } else {
                cm.sendOk("臭弟弟没钱在这装？.");
            }
            cm.dispose();
        }
        if (choose == 1) {
            num = parseInt(cm.getText());
            if (num < 0 || isNaN(num)) {
                cm.sendOk("输入非法.");
                cm.dispose();
            }
            cm.sendYesNo("购买 #b" + num + " #k条 鱼饵需要：#b" + (num * price1) + " 冒险币.？");
        }
        if (choose == 2) {
            num = parseInt(cm.getText());
            if (num < 0 || isNaN(num)) {
                cm.sendOk("输入非法.");
                cm.dispose();
            }
            cm.sendYesNo("购买 #b" + num + " #k条 高级鱼饵需要：#b" + (num * price2) + " 金币.？");
        }
        if (choose == 3) {
            price_st = calculate();
            if (price_st) {
                cm.sendYesNo("计算如下：\r\n" + calculate_text() + "\r\n\r\n#k共计：#b " + price_st + "#k 个硬币,是否兑换?");
            } else {
                cm.sendOk("你还没有一点产品喔.");
                cm.dispose();
            }
        }
        if (choose == 5) {
            cm.warp(910000000, 0);
            cm.dispose();
        }
    } else if (status == 3) {
        if (choose == 1) {
            if (cm.getMeso() >= num * price1 && cm.canHold(2300000)) {
                cm.gainMeso(-num * price1);
                cm.gainItem(2300000, num);
                cm.sendOk("购买成功.");
                cm.dispose();
            } else {
                cm.sendOk("冒险币不足或背包空间不足.");
                cm.dispose();
            }
        }
        if (choose == 2) {
            if (cm.getMeso() >= num * price2 && cm.canHold(2300001)) {
                cm.gainMeso(-num * price2);
                cm.gainItem(2300001, num);
                cm.sendOk("购买成功.");
                cm.dispose();
            } else {
                cm.sendOk("金币不足或背包空间不足.");
                cm.dispose();
            }
        }
        if (choose == 3) {
            // db.updateFishIntegral(price_st, player);
            cm.gainItem(4310248, price_st);
            calculate_del();
            cm.sendOk("成功获得硬币.");
            cm.dispose();
        }
    }
}

var fish = [
    //能钓上来的鱼，价格
    [4031627, 1],
    [4031628, 1],
    [4031630, 1],
    [4031631, 1],
    [4031633, 1],
    [4031634, 1],
    [4031635, 1],
    [4031636, 1],
    [4031637, 1],
    [4031638, 1],
    [4031639, 1],
    [4031640, 1],
    [4031641, 1],
    [4031642, 2],
    [4031643, 2],
    [4031644, 2],
    [4031645, 2],
    [4031646, 2],
    [4031647, 2],
    [4031648, 2]
];

function calculate_text() {
    var str = '';
    for (var i = 0; i < fish.length; i++) {
        if (cm.itemQuantity(fish[i][0])) {
            str += "\t#b>>#z" + fish[i][0] + "# x " + cm.itemQuantity(fish[i][0]) + ",换取：" + cm.itemQuantity(fish[i][0]) * fish[i][1] + " 硬币.\r\n";
        }
    }
    return (str == '') ? "暂时无." : str;
}

function calculate_del() {
    for (var i = 0; i < fish.length; i++) {
        cm.gainItem(fish[i][0], -cm.itemQuantity(fish[i][0]));
    }
    return true;
}

function calculate() {
    var count = 0;
    for (var i = 0; i < fish.length; i++) {
        count += cm.itemQuantity(fish[i][0]) * fish[i][1];
    }
    return count;
}